<?php
/**
 * The form for updating a phase.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-11-20
 */
require_once "login_info.php";
require_once "order.php";
require_once "phase.php";
require_once "machines.php";

loginInfo::update_login();

if (isset($_GET)) {
	$orderid = $_GET['id'];
}
?>

<html>
	<head>
<?php printf ("\t\t<title>Opol - Update Phases - %d</title>\n", (int)$orderid); ?>
	</head>
	<body>
		<link rel="stylesheet" type="text/css" href="opol.css">
		<?php require 'menu.php' ?>
		<br>
		<a href="new_phase.php?order_id=<?php print $orderid;?>">New Phase</a><br>
<?php
	if ($_POST) :
		if (isset ($_POST['phase_id'])) {
			$db = new database();

			$phaseid = $_POST['phase_id'];
			$phase = new phase((int)$phaseid);
			$phase->init ($db);

			/* This doesn't change the database, but serves to test whether or not
			 * the user has the necessary privileges.
			 */
			$result = $phase->update_database_by_id ($db);
			if (!$result) {
				echo "You do not have sufficient privileges to perform this action.<br>\n";
			} else {
				// Get start time.
				// Get stop time.
				$start_time = phase::get_date_time ($_POST, 'start');
				$stop_time = phase::get_date_time ($_POST, 'stop');
				// Get machine.
				$phase->machId = $_POST['machine'];
				$phase->keyword = $_POST['keyword'];
				// Get materials.

				$phase->time_req = phase::calculate_approx_time ($db, $phase->keyword);

				if ($start_time) {
					$phase->record_start_time ($start_time);
				}

				if ($stop_time) {
					$phase->record_stop_time ($stop_time);
				}

				$result = $phase->update_database_by_id ($db);
				print "Database Updated.<br>\n";
			}
			$db->close ();
		} else {
			$phase_id = $_POST['name'];
			if (!$phase_id) {
			} else {
				$db = new database ();
				$phase = new phase ($phase_id);
				$phase->init ($db);

				$mats = $phase->collect_materials ($db);

				$db->close ();
			}
		}

		$keyword = $phase->keyword;
		$mach = $phase->machId;
		$mats = $phase->collect_materials ();

?>
		<form id="phase" action="update_phase.php?id=<?php print $orderid;?>" method="post">
	<input type="hidden" name="phase_id" value="<?php print $phase_id;?>">
			<table>
				<tr>
	<td>Machine:</td>
	<td>
<?php
	$db = new database ();

/* Machine */
	echo "<select name='machine'>\n";
	$machs = $db->query("SELECT id, name FROM Machines");		 
	while ($row = $machs->fetch_assoc()) {
		$mid = $row['id']; $mname = $row['name'];
		if ($mid == $mach)
			print "\t\t\t\t\t\t<option value='$mid' selected>$mname</option>\n";
		else
			print "\t\t\t\t\t\t<option value='$mid'>$mname</option>\n";
	}
	$machs->free ();
	echo "</select>\n</td>\n";
echo "</td>\n";

/* Keyword */
echo "<tr>
<td>Keyword:</td>
<td><input type='text' name='keyword' value='$keyword'></td>
</tr>
";

/* Start/Stop Time */
$phase->print_date_time('start');
$phase->print_date_time('stop');

/* Materials */
?>

	<table id='input_table'>
	<tr>
		<th> Name </th>
		<th> Quantity </th>
	</tr>
<?php
	$materials = $db->query ("SELECT id, name FROM Materials");
	while ($row = $materials->fetch_assoc()) {
		$id = $row['id']; $name = $row['name'];
		print "\t\t\t\t\t<tr>\n";
		print "\t\t\t\t\t\t<td>$name</td>\n";
		print "\t\t\t\t\t\t<td><input type='text' name='mats_qt[$id]'";
		if (isset ($mats[$id])) {
			print "value='$mats[$id]'";
		}
		print "></input></td>\n";
		print "\t\t\t\t\t</tr>\n";
	}
$db->close ();
?>
</table>
			<tr>
				<td><input type="submit" value="Go"><br></td>
			</tr>
		</table>
		</form>
<?php else:?>

		<form id="phase" action="update_phase.php?id=<?php print $orderid;?>" method="post">
			<select name='name'>
		<?php 
		$db = new database ();
		$order = new order ($orderid);
		$order->init ($db);
		$phases = $order->collect_phases ($db);

		for ($i = 0; $i < count ($phases); $i++) {
			$phase = $phases[$i];
			$id = $phase->id ();
			$pri = $phase->priority;
			echo "<option value='$id'>$pri</option>\n";
		}
?>
			</select><br>
<input type="submit" value="Go"><br>
		</form>
<?php endif ?>
	</body>
</html>
